//
//  TLUpDataViewController.m
//  Home
//
//  Created by ffffff on 16/4/18.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "TLUpDataViewController.h"
#import "YHYDesignecell.h"
#import "WSHeaderActView.h"
#import "WSCustomPickerView.h"
#import "DCPaymentView.h"
#define POSTHEAD  @"http://114.215.240.173/api/index.php/Home/Index/Faceupload"
//#import "WSShopInfoNextView.h"

@interface TLUpDataViewController ()<UITableViewDataSource,UITableViewDelegate,WSHeaderActViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,WSShopInfoNextViewDelegate,PersonDetailsDelegate,WSCustomPickerViewDelegate>

{
    
    id returnData;
    NSString *personSex;
    NSMutableDictionary *params;
    NSIndexPath *indepath;
    ResignModel *model;
    NSMutableArray * allModel;
    BOOL man;
    
}
@property (nonatomic,strong)  UITableView *myTableView;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) UIImage *headImage;
@property (nonatomic, strong) WSHeaderActView *headerAV;
@property (nonatomic, strong) WSCustomPickerView *customPicker;
@property (nonatomic, strong) NSMutableArray *contentAry;
@property (nonatomic,strong)  UITextField *nameTextField;
@property (nonatomic,strong) NSString *addressStr;

@end

@implementation TLUpDataViewController


-(void) viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden=NO;
    self.tabBarController.tabBar.hidden=YES;
    
}

-(void) viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden=NO;
    self.tabBarController.tabBar.hidden=NO;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"基本信息";
    self.addressStr = @"选填";
    
    params = [[NSMutableDictionary alloc]init];
    [params setObject:self.sex forKey:@"sex"];

    allModel = [NSMutableArray new];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.view.backgroundColor = LV;
    self.array = [NSArray arrayWithObjects:@"头像", @"性别",@"姓名", @"微信号", @"常用邮箱",@"常用地址", nil];
    self.contentAry = [NSMutableArray arrayWithObjects: @"必填",@"选填", @"选填", @"选填",  nil];
    
    self.myTableView = [UITableView new];
    
    [self.view addSubview:self.myTableView];
    self.myTableView.rowHeight = ROWHEIGHT;
    // self.myTableView.sectionFooterHeight = 10;
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0 * AUTO_HEIGHT);
        make.left.mas_equalTo(self.view);
        make.height.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view);
        
    }];
    
    
    _myTableView.scrollEnabled = NO;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.backgroundColor = LV;
    //[self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView registerClass:[YHYDesignecell class] forCellReuseIdentifier:@"YHYOwnercell"];
    [[UITableViewHeaderFooterView appearance] setTintColor:LV];
    

    [self getBasicInfo];
    //  NSLog(@"!!!!!!!!!!!%@",_myBigdic);
    [_myTableView reloadData];
    _myBigdic = [NSMutableDictionary dictionary];

}


// 选中照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    _headImage = [info objectForKey:UIImagePickerControllerEditedImage];
    NSLog(@"头像%@",_headImage);
    [self updateHeadImage:_headImage andType:nil];

    
    [picker dismissViewControllerAnimated:YES completion:^{
        
        [self getBasicInfo];
        [_myTableView reloadData];
        
    }];
    
}
-(void)sendMyMessages:(BOOL)isMan{
    NSLog(@"man======%d",isMan);
    NSLog(@"oooooooo=%@",params);
    
 
    
    if (isMan == 1) {
        
        [params setObject:@"男" forKey:@"sex"];
        
    }
    else if (isMan == 1) {
        [params setObject:@"女" forKey:@"sex"];
        
    }
    NSLog(@"oooooooo=%@",params);


}



////// 网服务器 上传 基本信息
//-(void)uploadInformation{
//    NSString *code = [LOCALSEVE objectForKey:@"MCODE"];
//    NSString *uid = [LOCALSEVE objectForKey:@"UID"];
//
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    manager.requestSerializer  = [AFHTTPRequestSerializer serializer];
//    [manager POST:[NSString stringWithFormat:@"http://121.40.144.148/v1/Gafaer/Basic/Faceupload"] parameters:@{@"mcode":code,@"uid":uid}
//     constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//        
//        NSLog(@"nnnnnnnn==%@",formData);
//    } success:^(NSURLSessionDataTask *task, id responseObject) {
//        
//        NSLog(@"mmmmmmmm==%@",responseObject);
//        
//    } failure:^(NSURLSessionDataTask *task, NSError *error) {
//        
//        
//    }];
//    
//}


//我的页面跳到个人信息
- (void)getBasicInfo{
    NSString *url_string = [NSString stringWithFormat:@"%@%@",JIAFANURL,@"Gafaer/Basic/getBasicInfo"];
    NSString *code = [LOCALSEVE objectForKey:@"MCODE"];
    NSString *uid = [LOCALSEVE objectForKey:@"UID"];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:url_string parameters:@{@"mcode":code,@"uid":uid} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        model = [[ResignModel alloc]init];

//        NSLog(@"!!!!!!!!!!!%@",responseObject);
        [model setValuesForKeysWithDictionary:[responseObject objectForKey:@"data"]];
        NSLog(@"headerimag====%@",model.headpic);
        self.contentAry = [NSMutableArray arrayWithObjects:model.wechat,model.email,model.name,[NSString stringWithFormat:@"%@ %@ %@",model.province,model.city,model.area], nil];

            _addressStr = [NSString stringWithFormat:@"%@ %@ %@",model.province,model.city,model.area, nil];
        [allModel addObject:model];
        [_myTableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
}








- (void)updateHeadImage:(UIImage *)headImage andType:(NSString *)type{
    NSData *data;
    NSDictionary *paras;
    if (type == nil) {
        data = UIImageJPEGRepresentation(headImage, 0.75);

        paras = @{@"uid":[LOCALSEVE objectForKey:@"UID"] ,@"mcode":[LOCALSEVE objectForKey:@"MCODE"]};
    } else {
        data = UIImagePNGRepresentation(headImage);
        paras = @{@"uid":[LOCALSEVE objectForKey:@"UID"] ,@"mcode":[LOCALSEVE objectForKey:@"MCODE"]};
    }
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer  = [AFHTTPRequestSerializer serializer];
    
    [manager POST:[NSString stringWithFormat:@"http://121.40.144.148/v1/Gafaer/Basic/Faceupload"] parameters:paras constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        [formData appendPartWithFileData:data name:@"file" fileName:@"file.jpg" mimeType:@"image/jpeg"];
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSLog(@"上传图片成功");
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"上传图片失败:%@",error);

    }];
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }
    if (section == 1) {
        return 3;
    }
    else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    NSLog(@"ddddd=%@",self.contentAry);
    
    
    static NSString *cellstr = @"YHYOwnercell";
    YHYDesignecell *cell = [tableView dequeueReusableCellWithIdentifier:cellstr];

    [cell getResignModel:model];

    cell.messageDelegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //    [cell getResignModel:model];
    // if (cell == nil) {
    //    cell = [[YHYOwnercell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellstr];
    cell.string = _array[indexPath.row + indexPath.section * 1];
    
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {

//            cell.image = _headImage;
            [cell getResignModel:model];
            cell.type = 2;
            
        }
        if (indexPath.row == 1) {
            cell.type = 3;
            
            
        }
        if (indexPath.row == 2) {
            
            cell.type = 1;
            cell.content = self.contentAry[indexPath.row];
            
            
        }
    }
    if (indexPath.section == 1) {
        cell.string = _array[indexPath.row+ 1 + indexPath.section * 2];
        if (indexPath.row == 0) {
            cell.type = 1;
            cell.content = self.contentAry[indexPath.row];
        }
        if (indexPath.row == 1) {
            cell.type = 1;
            cell.content = self.contentAry[indexPath.row];
        }
        if (indexPath.row == 2) {
            cell.type = 1;
            cell.content = self.addressStr;
        }
        // }
    }
    
    
    return cell;
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0 && indexPath.section == 0) {
        
        [self.headerAV show];
    }
    //    else if (indexPath.row == 1 && indexPath.section == 1) {
    //
    //        [self.customPicker showPicker];
    //    }
    if (indexPath.row == 1 && indexPath.section == 0) {
        
    }
    if (indexPath.section == 0 && indexPath.row == 2) {
        DCPaymentView *payAlert = [DCPaymentView new];
        payAlert.title = [NSString stringWithFormat:@"请输入%@",_array[indexPath.row]];
        payAlert.numOfContent=5;
        [payAlert show];
        payAlert.completeHandle = ^(NSString *inputPwd) {
            
            
            self.contentAry[indexPath.row] = inputPwd;
            

            NSIndexPath *indexPath =[NSIndexPath indexPathForRow:2 inSection:0];
            [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];

            
        };
        
        
    }
    if (indexPath.section == 1 && indexPath.row == 0) {
        DCPaymentView *payAlert = [DCPaymentView new];
        NSString *str = [_array objectAtIndex:indexPath.row+ 2 + indexPath.section];
        payAlert.numOfContent=20;
        payAlert.title = [NSString stringWithFormat:@"请输入%@",str];
        [payAlert show];
        payAlert.completeHandle = ^(NSString *inputPwd) {
            
            self.contentAry[indexPath.row] = inputPwd;

            NSIndexPath *indexPath =[NSIndexPath indexPathForRow:0 inSection:1];
            [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
            
        };
    }
    
    if (indexPath.section == 1 && indexPath.row == 1) {
        DCPaymentView *payAlert = [DCPaymentView new];
        NSString *str = [_array objectAtIndex:indexPath.row+ 2 + indexPath.section];
        payAlert.numOfContent=20;
        payAlert.title = [NSString stringWithFormat:@"请输入%@",str];
        [payAlert show];
        payAlert.completeHandle = ^(NSString *inputPwd) {
            
            self.contentAry[indexPath.row] = inputPwd;
            
            NSIndexPath *indexPath =[NSIndexPath indexPathForRow:1 inSection:1];
            [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
            
        
        };
        
    }
    if (indexPath.section == 1 && indexPath.row == 2) {
        
        
        [self addCustomPickerView];
        //        [self.customPicker showPicker];
        
        
    }
    
    
}
-(void)addCustomPickerView{
    self.customPicker = [[WSCustomPickerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.customPicker.titleStr = @"商家地址";
    self.customPicker.delegate = self;
    [self.view addSubview:self.customPicker];
    [self.customPicker showPicker];
}


#pragma mark - 选择器
- (WSCustomPickerView *)customPicker {
    
    if (!_customPicker) {
        self.customPicker = [[WSCustomPickerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.customPicker.titleStr = @"商家地址";
        self.customPicker.delegate = self;
        [self.view addSubview:self.customPicker];
        
    }
    
    return _customPicker;
}

- (void)sendAddressInfo:(NSString *)address{
    NSLog(@"dddd=%@",address);
    _addressStr = address;

    NSIndexPath *indexPath =[NSIndexPath indexPathForRow:2 inSection:1];
    [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
 
}
#pragma mark - 头像点击View

- (WSHeaderActView *)headerAV {
    
    if (!_headerAV) {
        self.headerAV = [[WSHeaderActView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.headerAV.delegate = self;
        [self.view addSubview:_headerAV];
    }
    
    return _headerAV;
}
#pragma mark - headerAV 点击事件代理
- (void)presentImagePicker:(OPENIMAGE)img {
    
    if (img == 0) {
        [self selectCamera];
    } else if (img == 1) {
        [self selectPics];
    } else {
        
    }
}
- (void)selectCamera {
    
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
    picker.delegate = self;
    picker.allowsEditing = YES;//设置可编辑
    picker.sourceType = sourceType;
    [self presentViewController:picker animated:YES completion:nil];//进入照相界面
}

- (void)selectPics {
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    [self presentViewController:imagePicker animated:YES completion:nil];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (section == 1) {
        
        return 8 * AUTO_HEIGHT;
    }
    
    if (section == 2) {
        return 150 * AUTO_HEIGHT;
    }
    if (section == 3) {
        return 300 * AUTO_HEIGHT;
    }
    
    return 8 * AUTO_HEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 1;
}

-(void)Back:(UIButton *) goback
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 2) {
        
        
        _InfoNextView = [[WSShopInfoNextView alloc] initWithFrame:CGRectMake(0, 1000* AUTO_HEIGHT, SCREEN_WIDTH, 40 * AUTO_HEIGHT)];
        _InfoNextView.delegate = self;
        
        return _InfoNextView;
    }
    return nil;
    
}
-(void)postMymessages{
    
    
    NSLog(@"111111111=%@",self.contentAry);
    NSLog(@"aaaaaaaaa=%@",self.addressStr);
    NSArray *addressArray = [self.addressStr componentsSeparatedByString:@" "];
    NSLog(@"rrrrr=%@",addressArray);
    NSLog(@"ccccc=%ld",addressArray.count);

    
    NSString *weixin =  self.contentAry[0];
    NSString *youxiang = self.contentAry[1];
    NSString *name = self.contentAry[2];
    [params setObject:[LOCALSEVE objectForKey:@"UID"] forKey:@"uid"];
    [params setObject:[LOCALSEVE objectForKey:@"MCODE"] forKey:@"mcode"];
    [params setObject:weixin forKey:@"wechat"];
    [params setObject:youxiang forKey:@"email"];
    [params setObject:name forKey:@"name"];

    [params setObject:[LOCALSEVE objectForKey:@"SID"] forKey:@"sortid"];

    [params setObject:addressArray[0] forKey:@"province"];
    [params setObject:addressArray[1] forKey:@"city"];
    [params setObject:addressArray[2] forKey:@"area"];
    
    NSString *url_string = [NSString stringWithFormat:@"http://121.40.144.148/v1/Gafaer/Basic/UpdateBinfo"];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    NSLog(@"params ===== %@",params);
    [manager POST:url_string parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSLog(@"fffff%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"ggggggg%@",error);
        
    }];
    

    
    
    
    

    
}





- (void)nextAction{
    
   
    
        [self postMymessages];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeNameNotification" object:self userInfo:@{@"select":@"1"}];//1
    
    [self.navigationController popViewControllerAnimated:YES];
        
    
}




@end
